//
//  WBTitleButtun.swift
//  CustomWeiBo
//
//  Created by laiqiang on 21/5/20.
//  Copyright © 2021年 laiqiang. All rights reserved.
//

import UIKit

class WBTitleButtun: UIButton {

    init(title: String?) {
        super.init(frame: CGRect())
        
        if title == nil {
            setTitle("首页", for: .normal)
        }
        else{
            setTitle(title! + " ", for: .normal)
            setImage(UIImage.init(named: "navbar_arrow_down"), for: .normal)
            setImage(UIImage.init(named: "navbar_arrow_up"), for: .selected)
        }
        titleLabel?.font = UIFont.systemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray, for: .normal)
        sizeToFit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        print("调整布局")
        
        guard let titleLabel = titleLabel,let imageView = imageView else {
            return
        }
        
        if imageView.frame.origin.x < titleLabel.frame.origin.x {
            print("titleLabel.frame == \(titleLabel.frame) ,imageView.frame == \(imageView.frame)")
            titleLabel.frame = titleLabel.frame.offsetBy(dx: -imageView.bounds.width, dy: 0)
            imageView.frame = imageView.frame.offsetBy(dx: titleLabel.bounds.width, dy: 0)
        }
    }
}
